$(function(){
	zongjia = 0;
	$(window).scroll(function(){


	var ST = $(window).scrollTop();


	if(ST>226){
		$('.classify').css({
			"position": "fixed","top":"0px","z-index":"9999","margin-top":"0","border-top":"2px solid #ff2d4b","box-shadow":"0 1px 5px 0 rgba(0, 0, 0, 0.4)"
		});
	}

	if (ST<226) {
		$('.classify').css({
			"position": "relative","z-index":"0","margin-top":"20px","border-top":"none","box-shadow":"none"
		});
	};



	});

	$.ajax({
        url:WEB+'?c=Content&a=ifSession&stid='+stid,
            type:"post",
            data:{shopSession:'shopSession'},
            dataType:"json",
            success:function(data){
            var str = '';
            $.each(data,function(k,v){
          	$.each(v,function(key,value){
			str +='<tr id="'+value['id']+'" class="item normal-itemhide"style="display: table-row;">\
			<td class="item-name">'+value['name']+'</td>\
			<td class="item-count">\
			<input type="hidden" value="'+key+'">\
			<span class="item-minus" value="" type="button"></span>\
			<input class="item-count" type="input" value="'+value['num']+'">\
			<span class="item-plus" value="" type="button"></span>\
			</td>\
			<td class="item-price">¥<span>'+value['price']+'</span></td>\
			<td class="item-delete">\
			<span class="item-delete-icon">×</span>\
			</td>\
			</tr>';

			$('.greens li[data='+value['id']+']').find('.select_count').html(''+value['num']+'');
			if(value.num==0){

				$('.greens li[data='+value['id']+']').find('.addition').css({"width":"30px"});
			}else{
				$('.greens li[data='+value['id']+']').find('.addition').css({"width":"92px"});
			}
			
		});	
          	zongjia = data.total;
			$('.premium-origin del').html(zongjia);

			jiesuan = zongjia-peisong-mian;
			if(jiesuan<=0){
				jiesuan = zongjia-peisong;
			}

			// 如果结算小于起送价  就显示还需要多少金额才配送
			if(jiesuan<qisong){
				xy=qisong-jiesuan;
				$('#cartCha').html('差 ¥'+xy+'起送');
			}

			$('#cartAmout .premium-price').html('共 ¥'+jiesuan+'元');

			// 如果结算金额大于起送价的时候，就显示选好按钮
			
			if(jiesuan>qisong){
				$('#cartCha').hide();
				$('#cartSubmit').show();
				$('#cartSubmit').click(function(){
					
					location.href=''+MODULE+'/Oreder/index&stid='+stid+'';
				});
			}

		});
            $('#shopping .item-list').prepend(str);
            $('#shopping .cc-warp').show();
            	


			}

    });

	// 添加到购物车
	//添加
	
	$('.addition_con .addtocart').click(function(){
			
			$(this).parents('.addition').css({"width":"92px"});
			defaultNub = $(this).parents('.addition').find('.select_count').html();
			defaultNub = parseInt(defaultNub);
			$(this).parents('.addition').find('.select_count').html(defaultNub+1);

			var shopId = $(this).parents('li').attr('data');

			var shupNub = defaultNub;
			

		$.ajax({
        url:WEB+'?c=Content&a=ajax_shop&stid='+stid,
            type:"post",
            data:{'shopId':shopId,'shupNub':shupNub},
            dataType:"json",
            success:function(data){

            var str = '<tr id="'+data.sid+'" class="item normal-itemhide"style="display: table-row;">\
			<td class="item-name">'+data.shopname+'</td>\
			<td class="item-count">\
			<input type="hidden" value="'+data.sid+'">\
			<span class="item-minus" value="" type="button"></span>\
			<input class="item-count" type="input" value="'+data.nub+'">\
			<span class="item-plus" value="" type="button"></span>\
			</td>\
			<td class="item-price">¥<span>'+data.price+'</span></td>\
			<td class="item-delete">\
			<span class="item-delete-icon">×</span>\
			</td>\
			</tr>';
			if($('#'+data.sid+'').length>0){
				$('#'+data.sid+' .item-count').val(''+data.nub+'');
			}else{
				$('#shopping .item-list').prepend(str);
			}

			$('#shopping .cc-warp').show();
			// 总算
			zongjia =zongjia+parseInt(data.price);
			//把值插入到标签中
			$('.premium-origin del').html(zongjia);
			// 结算
			// 如果购买的金额小于优惠减夹的钱，就让它直接跟配送的钱相减
			jiesuan = zongjia-peisong-mian;
			if(jiesuan<=0){
				jiesuan = zongjia-peisong;
			}

			// 如果结算小于起送价  就显示还需要多少金额才配送
			if(jiesuan<qisong){
				xy=qisong-jiesuan;
				$('#cartCha').html('差 ¥'+xy+'起送');
			}

			$('#cartAmout .premium-price').html('共 ¥'+jiesuan+'元');

			// 如果结算金额大于起送价的时候，就显示选好按钮
			
			if(jiesuan>qisong){
				$('#cartCha').hide();
				$('#cartSubmit').show();
			}

			
				

			
			
          
            
             
            
            }

    });
	});


	//  减
	
	$('.addition_con .minusfrcart').click(function(){
			
			var defaultNub = $(this).parents('.addition').find('.select_count').html();

			

			defaultNub = parseInt(defaultNub);

			if(defaultNub>0){
			$(this).parents('.addition').find('.select_count').html(defaultNub-1);
			}else if(defaultNub<=1){
				$(this).parents('.addition').css({"width":"30px"});
			}

			var shopId = $(this).parents('li').attr('data');

			var shupNub = defaultNub-1;


			$.ajax({
        url:WEB+'?c=Content&a=ajax_shopimg&stid='+stid,
            type:"post",
            data:{'shopId':shopId,'shupNub':shupNub},
            dataType:"json",
            success:function(data){

				$('#'+data.sid+' .item-count').val(''+data.nub+'');
				// 总算
				if(data.nub==0){
				$('.greens li[data='+data['sid']+']').find('.addition').css({"width":"30px"});
			}
			zongjia =zongjia-parseInt(data.price);
			//把值插入到标签中
			$('.premium-origin del').html(zongjia);
			// 结算
			// 如果购买的金额小于优惠减夹的钱，就让它直接跟配送的钱相减
			jiesuan = zongjia-peisong-mian;
			if(jiesuan<=0){
				jiesuan = zongjia-peisong;
			}

			// 如果结算小于起送价  就显示还需要多少金额才配送
			if(jiesuan<qisong){
				xy=qisong-jiesuan;
				$('#cartCha').html('差 ¥'+xy+'起送');
			}

			$('#cartAmout .premium-price').html('共 ¥'+jiesuan+'元');

			// 如果结算金额大于起送价的时候，就显示选好按钮
			
			if(jiesuan>qisong){
				$('#cartCha').hide();
				$('#cartSubmit').show();
			}
		}

    });

});
	

})